package com.fbs.wowo.home.controller;

import com.fbs.wowo.base.BaseController;
import com.fbs.wowo.base.Response;
import com.fbs.wowo.correctRight.entity.CorrectRightInfo;
import com.fbs.wowo.correctRight.service.CorrectRightService;
import com.fbs.wowo.essay.entity.Essay;
import com.fbs.wowo.essay.mapper.EssayMapper;
import com.fbs.wowo.exception.NoSuchDataException;
import com.fbs.wowo.manage.entity.User;
import com.fbs.wowo.manage.mapper.UserMapper;
import com.fbs.wowo.util.common.Constants;
import com.fbs.wowo.util.common.Validator;
import com.fbs.wowo.works.entity.Works;
import com.fbs.wowo.works.mapper.WorksMapper;
import com.fbs.wowo.wx.entity.WxAuth;
import com.fbs.wowo.wx.mapper.WxAuthMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/")
public class HomeController extends BaseController {

    @RequestMapping("/")
    public String main() {
        return "redirect:" + Constants.getURL() + "/main.html";
    }

    @RequestMapping("/index")
    public String index() {
        return  "index";
    }

    /**
     * @param model
     * @param essayId
     * @return
     */
    @RequestMapping("/dataDetail")
    public String getDataDetail(Model model,@RequestParam String essayId,@RequestParam int type) {
        CorrectRightInfo correctRight = correctRightService.findCorrectRightInfoByEssayId(essayId);
        if (correctRight!=null){
            model.addAttribute("correctRight", correctRight);
            if (type==1){
                Essay essay = essayMapper.selectByPrimaryKey(essayId);
                model.addAttribute("hashValue", essay.getHashValue());
                return "dataDetail";
            }else if (type==2){
                Works works = worksMapper.selectByPrimaryKey(essayId);
                model.addAttribute("hashValue", works.getHashValue());
                return "dataDetail";
            }
        }
        return "error";
    }

    /**
     * @param model
     * @param essayId
     * @return
     */
    @RequestMapping("/wx/dataDetail")
    public String getWxDataDetail(Model model,@RequestParam String essayId) {
        WxAuth wxAuth = wxAuthMapper.selectByPrimaryKey(essayId);
        model.addAttribute("wxAuth", wxAuth);
        return "wx_dataDetail";
    }

    /**
     * 分享页面
     */
    @GetMapping("/share")
    @ResponseBody
    public Response share(Model model, @RequestParam String productId, @RequestParam(required = false, defaultValue = "1") Byte productType) {
        Validator.verifyStr(productId);
        Essay essay = essayMapper.selectByPrimaryKey(productId);
        if (essay == null) throw new NoSuchDataException();
        User user = userMapper.findByUserId(essay.getUserId());
        if (user == null) throw new NoSuchDataException();
        essay.setUserId(user.getName());
        return success(essay);
    }

    @Resource
    private CorrectRightService correctRightService;

    @Resource
    private EssayMapper essayMapper;

    @Resource
    private WxAuthMapper wxAuthMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private WorksMapper worksMapper;
}
